package com.ww.core.widget.wheel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.ww.core.R;
import com.ww.core.util.Logger;
import com.ww.core.util.StringUtils;
import com.ww.core.widget.wheel.widget.NumericWheelAdapter;
import com.ww.core.widget.wheel.widget.WheelView;

public class WheelTime {

	private Context context;
	private Dialog dlg;

	private WheelView hours, mins;
	private Button sure, cancel;
	private String time;
	private String result;
	private TextView title;

	public WheelTime(Context context) {
		this.context = context;
	}

	/**
	 * 公共对话框.
	 * @param title 标题
	 * @param content 内容
	 * @param okClick 确定按钮点击事件.
	 * @return AlertDialog
	 */
	public void show(String time, OnClickListener click) {
		this.time = time;
		dlg = new Dialog(context, R.style.MyDialog);
		dlg.show();
		Window window = dlg.getWindow();
		window.setContentView(R.layout.core_wheel_2);
		findView(window);

		sure.setOnClickListener(click);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				result = null;
				cancel();// 姝ゅ涓?畾瑕佽皟鐢╢inish()鏂规硶
			}
		});
	}

	private void findView(Window window) {
		title = (TextView) window.findViewById(R.id.title);
		hours = (WheelView) window.findViewById(R.id.wheel2_1);
		mins = (WheelView) window.findViewById(R.id.wheel2_2);
		sure = (Button) window.findViewById(R.id.buttonsure);
		cancel = (Button) window.findViewById(R.id.buttoncancle);
		if (StringUtils.isNotEmpty(time)) {
			title.setText(time);
			title.setVisibility(View.VISIBLE);
		}
		initiWheelView();
	}

	public String getResult() {
		StringBuffer sb = new StringBuffer();
		sb.append((hours.getCurrentItem())).append(":").append((mins.getCurrentItem()));
		result = formatDateTime(sb.toString());
		return result;
	}

	/**
	 * 格式化日期.
	 * @param dateTime  日期
	 * @return 格式化后的日期
	 */
	private String formatDateTime(String dateTime) {
		SimpleDateFormat formatYMD = new SimpleDateFormat("HH:mm");// formatYMD表示的是yyyy-MM-dd格式
		Date d = null;
		try {
			d = formatYMD.parse(dateTime);
		} catch (ParseException e) {
			Logger.error("格式化日期", e);
		}
		return formatYMD.format(d);
	}

	private void initiWheelView() {
		hours.setAdapter(new NumericWheelAdapter(0, 23));
		hours.setLabel("时");

		mins.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));
		mins.setLabel("分");
		mins.setCyclic(true);

		// set current time
		if (!StringUtils.isNotEmpty(time)) {
			Calendar c = Calendar.getInstance();
			int curHours = c.get(Calendar.HOUR_OF_DAY);
			int curMinutes = c.get(Calendar.MINUTE);

			hours.setCurrentItem(curHours);
			mins.setCurrentItem(curMinutes);
		} else {
			String str[] = time.split(":");
			int h = Integer.parseInt(str[0]);
			int m = Integer.parseInt(str[1]);
			hours.setCurrentItem(h);
			mins.setCurrentItem(m - 1);
		}
	}
	public void cancel() {
		if (dlg != null) {
			dlg.cancel();
		}
	}
}
